package at.fhj.swd.pse.gui.pageobjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import at.fhj.swd.pse.gui.drivers.GuiTestBaseSeleniumJava4Webdriver;

/**
 * Get community page and insert a new community
 * 
 * @author Michaela Maierhofer
 */

public class CommunityPage extends GuiTestBaseSeleniumJava4Webdriver {
	private final GuiTestBaseSeleniumJava4Webdriver webdriver = new GuiTestBaseSeleniumJava4Webdriver();

	public GuiTestBaseSeleniumJava4Webdriver getWebdriver() {
		return this.webdriver;
	}

	public CommunityPage() throws Exception {
		this.open();

	}

	public void open() throws Exception {
		this.webdriver.start();
		this.webdriver.getDriver().get(this.webdriver.getBaseUrl());
		this.webdriver.getDriver().findElement(By.linkText("Newcommunity")).click();

	}

	public void close() throws Exception {
		this.webdriver.end();
	}

	public boolean insertNewCommunity(final String name, final String description, final String topic, final String regExpSearchString) throws Exception {
		this.webdriver.getDriver().findElement(By.id("register:name")).clear();
		this.webdriver.getDriver().findElement(By.id("register:name")).sendKeys(name);
		this.webdriver.getDriver().findElement(By.id("register:description")).clear();
		this.webdriver.getDriver().findElement(By.id("register:description")).sendKeys(description);
		this.webdriver.getDriver().findElement(By.id("register:topic")).clear();
		this.webdriver.getDriver().findElement(By.id("register:topic")).sendKeys(topic);
		this.webdriver.getDriver().findElement(By.id("register:create")).click();

		WebElement dynamicElement = new WebDriverWait(this.webdriver.getDriver(), 10).until(ExpectedConditions.presenceOfElementLocated(By.id("loginOutputLabel")));
		return dynamicElement.getText().startsWith(regExpSearchString);
	}

}